﻿using Meiam.System.Common;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Meiam.System.Core
{
    public class ScadaDbContext
    {
        public SqlSugarClient ScadaDB;   //用来处理事务多表查询和复杂的操作

        public static SqlSugarClient Current
        {
            get
            {
                return new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = AppSettings.Configuration["ScadaDbContext:ConnectionString"],
                    DbType = (DbType)Convert.ToInt32(AppSettings.Configuration["ScadaDbContext:DbType"]),
                    IsAutoCloseConnection = false,
                    InitKeyType = InitKeyType.Attribute,
                    //ConfigureExternalServices = new ConfigureExternalServices()
                    //{
                    //    DataInfoCacheService = new RedisCache()
                    //},
                    MoreSettings = new ConnMoreSettings()
                    {
                        IsAutoRemoveDataCache = true
                    }
                });
            }
        }

        public ScadaDbContext()
        {
            try
            {
                ScadaDB = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = AppSettings.Configuration["ScadaDbContext:ConnectionString"],
                    DbType = (DbType)Convert.ToInt32(AppSettings.Configuration["ScadaDbContext:DbType"]),
                    IsAutoCloseConnection = true,
                    InitKeyType = InitKeyType.Attribute,
                    //ConfigureExternalServices = new ConfigureExternalServices()
                    //{
                    //    DataInfoCacheService = new RedisCache()
                    //},
                    MoreSettings = new ConnMoreSettings()
                    {
                        IsAutoRemoveDataCache = true
                    }
                });
                //调式代码 用来打印SQL 
                ScadaDB.Aop.OnLogExecuting = (sql, pars) =>
                {
                };
                ScadaDB.Open();
            }
            catch (Exception err)
            {

            }
            finally
            {
                ScadaDB.Close();
            }

        }
    }
}
